<?php
  
namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ZhuanlanController extends Controller
{
	/**
     * 专栏列表页
     *
     * @return data
     */
	public function getList(Request $request)
	{

		//判断用户是否搜索
		if($request->input('name')){
			//查询所有专栏信息
			$data = DB::table('column')
            ->leftJoin('topic','column.tid','=','topic.id')
            ->select('column.*', 'topic.title')
            ->where('column.cname','like','%'.$request->input('name').'%')
            ->where('examine','=','1')
            ->paginate(5);
		}else{
			//查询所有专栏信息
			$data = DB::table('column')
            ->leftJoin('topic', 'topic.id', '=', 'column.tid')
            ->select('column.*', 'topic.title')
            ->where('examine','=','1')
            ->paginate(5);

		}
		// dd($data);
		//获取所有参数
		$all = $request->all();

        //分配模板
		return view('/admin.zhuanlan.list',['data'=>$data,'all'=>$all]);
	}

	/**
     * 执行状态修改
     *
     * @return data
     */
	public function getUpdate(Request $request)
	{
		//获取要修改状态的id
		$id = $request->input('id'); 

		//查询status的值
		$data = DB::table('column')
				->where('id',$id)
				->first();
		//提取status
		$status = $data->status;

		//判断status是否为0,执行相应的操作
		if($status==0)
		{	
			//执行禁用操作
			$res = DB::table('column')->where('id',$id)->update(['status'=>1]);

			//判断是否禁用成功
			if($res)
	        {
	            return redirect('admin/zhuanlan/list')->with('success','禁用成功');
	        }else{
	            return back()->withInput()->with('error','禁用失败');
	        }
		}else{
			//执行启用操作
			$res = DB::table('column')->where('id',$id)->update(['status'=>0]);

			//判断是否启用成功
			if($res)
	        {
	            return redirect('admin/zhuanlan/list')->with('success','启用成功');
	        }else{
	            return back()->withInput()->with('error','启用失败');
	        }
		}
	}

	/**
     * 审核列表
     *
     * @return data
     */
	public function getExamine(Request $request)
	{
		//判断用户是否查询
		if($request->input('name')){
			//查询所有专栏信息
			$data = DB::table('column')
            ->leftJoin('topic','column.tid','=','topic.id')
            ->select('column.*', 'topic.title')
            ->where('column.cname','like','%'.$request->input('name').'%')
            ->where('examine','=','0')
            ->paginate(5);
		}else{
			//查询所有待审核的专栏
			$data = DB::table('column')
			->leftJoin('topic', 'topic.id', '=', 'column.tid')
	        ->select('column.*', 'topic.title')
			->where('examine','=','0')
			->paginate(5);
		}

		//获取所有参数
		$all = $request->all();

		//分配模板
		return view('/admin.zhuanlan.examine',['data'=>$data,'all'=>$all]);
	}

	/**
     * 执行审核
     *
     * @return data
     */
	public function getEdit(Request $request)
	{
		//获取要审核的id
		$id = $request->input('id');

		//执行审核操作
		$res = DB::table('column')->where('id',$id)->update(['examine'=>1]);

		//判断是否审核成功
		if($res)
        {
            return redirect('admin/zhuanlan/list')->with('success','审核成功');
        }else{
            return back()->withInput()->with('error','审核失败');
        }
	}

	/**
     * 专栏文章
     *
     * @return data all
     */
	public function getArticle(Request $request)
	{
		//获取专栏id
		$coid = $request->input('id');

		//查询数据
		$data = DB::table('column')
			->leftJoin('user','user.id','=','column.uid')
			->leftJoin('columnarticle','column.id','=','columnarticle.coid')
			->leftJoin('topic','topic.id','=','column.tid')
			->select('columnarticle.*','user.name','column.cname','topic.title')
			->where('coid',$coid)
			->paginate(5);
			
		//获取所有参数
		$all = $request->all();

		//分配模板
		return view('/admin.zhuanlan.article',['data'=>$data,'all'=>$all]);
	}

	/**
     * 查看专栏文章
     *
     * @return data
     */
	public function getViewarticle(Request $request)
	{
		//获取文章id
		$id = $request->input('id');

		//查询数据 
		$data = DB::table('columnarticle')
			->leftJoin('user','user.id','=','columnarticle.uid')
			->select('columnarticle.*','user.name')
			->where('columnarticle.id',$id)
			->get();
			
		//分配模板
		return view('/admin.zhuanlan.viewarticle',['data'=>$data]);
	}

	/**
     * 执行屏蔽文章操作
     *
     * @return data
     */
	public function getShield(Request $request)
	{
		//获取要屏蔽的文章id
		$id = $request->input('id');

		//查询cstatus的值
		$data = DB::table('columnarticle')
				->where('id',$id)
				->first();
				
		//提取status
		$cstatus = $data->cstatus;
	
		//判断status是否为0,执行相应的操作
		if($cstatus==0)
		{	
			//执行屏蔽操作
			$res = DB::table('columnarticle')->where('id',$id)->update(['cstatus'=>1]);

			//判断是否屏蔽成功
			if($res)
	        {
	            return redirect('admin/zhuanlan/zhuanlanarticlelist')->with('success','屏蔽成功');
	        }else{
	            return back()->withInput()->with('error','屏蔽失败');
	        }
		}else{
			//执行取消屏蔽操作
			$res = DB::table('columnarticle')->where('id',$id)->update(['cstatus'=>0]);

			//判断是否取消屏蔽成功
			if($res)
	        {
	            return redirect('admin/zhuanlan/zhuanlanarticlelist')->with('success','取消屏蔽成功');
	        }else{
	            return back()->withInput()->with('error','取消屏蔽失败');
	        }
		}

	}

	/**
     * 专栏文章列表
     *
     * @return data
     */
	public function getZhuanlanarticlelist(Request $request)
	{
		//判断用户是否搜索
		if($request->input('name')){
			//查询指定专栏文章
			$data = DB::table('columnarticle')
            ->leftJoin('column','columnarticle.coid','=','column.id')
            ->leftJoin('user','user.id','=','columnarticle.uid')
            ->leftJoin('topic','topic.id','=','columnarticle.tid')
            ->select('columnarticle.*','column.cname','user.name','topic.title')
            ->where('column.cname','like','%'.$request->input('name').'%')
            ->where('cstatus','=','0')
            ->paginate(5);
		}else{
			//查询所有专栏文章
			$data = DB::table('columnarticle')
            ->leftJoin('column','columnarticle.coid','=','column.id')
            ->leftJoin('user','user.id','=','columnarticle.uid')
            ->leftJoin('topic','topic.id','=','columnarticle.tid')
            ->select('columnarticle.*','column.cname','user.name','topic.title')
            ->where('cstatus','=','0')
            ->paginate(5);

		}

		//获取所有参数
		$all = $request->all();
		
		//分配模板
		return view('/admin.zhuanlan.zhuanlanarticlelist',['data'=>$data,'all'=>$all]);
	}

	/**
     * 专栏文章回收站列表
     *
     * @return data
     */
	public function getZhuanlanrecycle(Request $request)
	{
		//判断用户是否搜索
		if($request->input('name')){
			//查询指定专栏文章
			$data = DB::table('columnarticle')
            ->leftJoin('column','columnarticle.coid','=','column.id')
            ->leftJoin('user','user.id','=','columnarticle.uid')
            ->leftJoin('topic','topic.id','=','columnarticle.tid')
            ->select('columnarticle.*','column.cname','user.name','topic.title')
            ->where('column.cname','like','%'.$request->input('name').'%')
            ->where('cstatus','=','1')
            ->paginate(5);
		}else{
			//查询所有专栏文章
			$data = DB::table('columnarticle')
            ->leftJoin('column','columnarticle.coid','=','column.id')
            ->leftJoin('user','user.id','=','columnarticle.uid')
            ->leftJoin('topic','topic.id','=','columnarticle.tid')
            ->select('columnarticle.*','column.cname','user.name','topic.title')
            ->where('cstatus','=','1')
            ->paginate(5);

		}

		//获取所有参数
		$all = $request->all();
		
		//分配模板
		return view('/admin.zhuanlan.zhuanlanrecycle',['data'=>$data,'all'=>$all]);
	}
	
}
